#ifndef UART_H_
#define UART_H_
#include "stm32f10x_it.h"
#include "ucos_ii.h"

#define UART_NUM     (8)  
#define NUM_UART_485 (6)  

#define NUM_UART_HMI    (1)  
#define NUM_UART_COMM   (2)  
#define NUM_UART_SENSOR (1)   

#define UART1_CON (0) 
#define UART2_HMI (1)  

#define UART3_SENSOR (2)   
#define UART4_485    (3)  
#define UART5_485    (4) 

#define UART6_COMM (5)   

#define UART7_485 (6)   
#define UART8_485 (7)  

#define UDATA_7B    (0)
#define UDATA_8B    (1)
#define PARITY_NONE (0)
#define PARITY_ODD  (1)
#define PARITY_EVEN (2)
#define STOP_BIT1   (0)
#define STOP_BIT1X5 (1)
#define STOP_BIT2   (2)

#define UART_TICK_XNOW (5)
#define UART_TICK_OVER (2)

#define USART1_SWITCH 1
#define USART2_SWITCH 0
#define USART3_SWITCH 0
#define UART4_SWITCH  0
#define UART5_SWITCH  0
#define USART6_SWITCH 1
#define UART7_SWITCH  0
#define UART8_SWITCH  0

#define USART1_RX_BUFFER_SIZE 1024 /* 2,4,8,16,32,64,128 or 256 bytes */
#define USART1_TX_BUFFER_SIZE 1024 /* 2,4,8,16,32,64,128 or 256 bytes */
#define false                 0
#define true                  1
#define wrong                 0
#define right                 1
extern OS_EVENT*       USART1_rec_sem;
extern INT8U           USART1_RxBuf[USART1_RX_BUFFER_SIZE];
extern INT8U           USART1_Rec_Buf[USART1_RX_BUFFER_SIZE];   // 接收处理程序使用
extern INT8U           USART1_Rec_Num;
extern INT8U           USART1_Send_Buf[USART1_TX_BUFFER_SIZE];   // 发送处理程序使用
extern INT8U           USART1_Send_Num;
extern INT8U           USART1_checkoutError;
extern volatile INT8U  USART1_rx_overflow_flag;
extern volatile INT16U USART1_rx_counter;
extern volatile INT16U USART1_tx_counter;

void USART_Config(void);
void Time_Proc(void);

int UartInit(u8 port, u8 baud, u8 datBits, u8 parity, u8 stopBits);

/*
 * function: InitUartEvent
 * brief   : init uart event
 * param   : void
 * result  : 0:ok
 */
int InitUartEvent(void);

/*
 * function: ClearUartRx
 * brief   : clear uart rx buffer
 * param   : uartID[in]:uart-id
 * result  : -1:param error
 *            0:ok
 */
int ClearUartRx(int uartID);

/*
 * function: GetUartData
 * brief   : get all data from uart
 * param   : uartID[in]:uart id
 *           buf[in]:buffer for storing data
 *           size[in]:buffer size
 * result  : <0:param error
 *          >=0:data number
 */
// int GetUartData( int uartID,unsigned char *buf,int size );
int GetUartData(int uartID, uint8_t* buf, int size);

/*
 * function: SendUartData
 * brief   : send data to uart
 * param   : uartID[in]:uart id
 *           buf[in]:buffer for storing data
 *           iNum[in]:data number
 * result  : <0:param error
 *          >=0:data number
 */
int SendUartData(int uartID, unsigned char* buf, int iNum);

/*
 * function: SendUartStr
 * brief   : send string via uart
 * param   : uartID[in]:uart-id
 *           str[in]:string
 * result  : <0:send error
 *            0:ok
 */
int SendUartStr(int uartID, char* str);

/*
 * function: SendUartStrX
 * brief   : send string via uart
 * param   : uartID[in]:uart-id
 *           fmt[in]:string
 *           ...
 * result  : <0:send error
 *            0:ok
 */
int SendUartStrX(int uartID, char* fmt, ...);

/*
 * function: WaitUartIdle
 * brief   : wait uart bus idle
 * param   : uartID[in]:uart-id
 *           wToutTick[in]:timeout tick
 * result  : -1:param error
 *        other:event error
 */
int WaitUartIdle(int uartID, u16 wToutTick);
int ClearUartIdle(int uartID);

int SetGprsLinkSem(int cnt);

/*
 * function: WaitGprsLink
 * brief   : wait gprs-link
 * param   : wToutTick[in]:timeout tick
 * result  : -1:param error
 *        other:event error
 */
int    WaitGprsLink(u16 wToutTick);
INT8U  USART1_Transmit_Byte(INT8U data);
INT8U  USART1_Send_Proc(INT16U send_num);
INT16U USART1_Receive_Str(INT8U* data);

#endif
